package demo.java.lang.management.jvm;

import demo.dcs.LocalAsySendHolder;
import demo.java.lang.management.vo.DynamicJvmModel;
import demo.java.lang.management.vo.JavaProcess;
import demo.java.lang.management.vo.JsonUtils;
import demo.java.lang.management.vo.StartupInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author buffoon
 * @date 16/1/14
 */
public class JVMDumpTask extends Thread {

    private static Logger log = LoggerFactory.getLogger(JVMDumpTask.class);

    private static final JVM jvm = new JVM();

    private static String app;

    private static String hostname;

    /**
     * 默认10分钟dump一次
     */
    private static final int jvmMonitorTimeSpan =  10 * 60 * 1000;

    /**
     * 添加应用修改dump频率的接口，在应用有问题时进行
     */
    private static boolean jvmMonitor4App = false;
    private static final int jvmMonitorTimeSpan4App = 60 * 1000;

    private LocalAsySendImpl2 asySendService = LocalAsySendHolder.getInstance2();

    private static long jvmStartTs = System.currentTimeMillis();

    public JVMDumpTask() {
        this.setName("JVMDumpTask-agent-Thread");
    }

    @Override
    public void run() {
        if (jvm == null) {
            return;
        }

        app = AppTraceConfig.getLocalConfig().getAppName();
        hostname = AppTraceConfig.getLocalConfig().getHostName();

        try {
            StartupInfo startupPart = jvm.getStaticPartJavaProcess();
            if (startupPart != null) {
                jvmStartTs = startupPart.getStartJvmTime();
                StaticJvmModel staticJvmModel = new StaticJvmModel(app, hostname, startupPart);
                asySendService.jvmStaticInfoSends(JsonUtils.toJson(staticJvmModel));
            }
        } catch (Throwable e) {
            log.error("first get jvm StartupInfo error: " + e.getMessage(), e);
        }
//        int loopCount=0;

        for (; ; ) {
            try {
                try {
                    JavaProcess dynamicPart = jvm.getDynamicPartJavaProcess();
                    if (dynamicPart != null) {
                        DynamicJvmModel dynamicJvmModel = new DynamicJvmModel(app, hostname, dynamicPart);
//                        if (loopCount%10==0) {
                        	asySendService.asyMetricSends(JvmMetricUtil.getMetric(dynamicJvmModel));
                        	//防止溢出
//                        	loopCount = 0;
//						}
//                        asySendService.asyMetricSends(JvmMetricUtil.getFgcMetric(dynamicJvmModel,jvmStartTs));
                        asySendService.jvmDynamicInfoSends(ThreadEsUtil.getThreadInfos(dynamicJvmModel));
                    }
                } catch (Throwable e) {
                    log.error("JVMDumpTask dump jvminfo error", e);
                }
                try {
                    if(jvmMonitor4App){
                        Thread.sleep(jvmMonitorTimeSpan4App);
                    }else {
                        Thread.sleep(jvmMonitorTimeSpan);
                    }
                } catch (InterruptedException e) {
                    log.error("JVMDumpTask running: " + e.getMessage(), e);
                }
//                loopCount++;
            } catch (Throwable e) {
                log.error("JVMDumpTask dump jvminfo other error", e);
            }

        }
    }

    public static boolean isJvmMonitor4App() {
        return jvmMonitor4App;
    }

    public static void setJvmMonitor4App(boolean jvmMonitor4App) {
        JVMDumpTask.jvmMonitor4App = jvmMonitor4App;
    }

}
